package logic

import (
	"context"
	"fmt"
	"goods_srv/goodssrv"
	"goods_srv/internal/svc"
	"goods_srv/models/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodsLogic {
	return &AddGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddGoodsLogic) AddGoods(in *goodssrv.AddGoodsRequest) (*goodssrv.AddGoodsResponse, error) {
	// todo: add your logic here and delete this line

	goods := mysql.Goods{
		Title:  in.Title,
		Cover:  in.Cover,
		Price:  float64(in.Price),
		Stock:  int(in.Stock),
		Status: "审核中",
	}
	err := goods.AddGoods()
	if err != nil {
		return nil, fmt.Errorf("商品发布失败")
	}

	return &goodssrv.AddGoodsResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
